package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.mapper.AddressMapper;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class AddressServiceImpl implements AddressService {
	@Autowired
	private AddressMapper addressMapper;
	
	@Override
	public void delete(List<String> ids) {
		if (ids.size() > 0 && ids != null) {
			addressMapper.deleteBatchIds(ids);
		}
	}
	
	@Override
	public Address finById(String id) {
		Address address = new Address();
		address.setId(Long.valueOf(id));
		address = addressMapper.selectById(address);
		return address;
	}
	
	@Override
	public void update(Address address) {
		addressMapper.updateById(address);
	}
	
	@Override
	public Address findDefault() {
		LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
		
		//查找当前用户的默认地址
		wrapper.eq(Address::getUserId,UserHolder.getUser().getId())
				.eq(Address::getIsDefault,1);
		Address address = addressMapper.selectOne(wrapper);
		return address;
	}
	
	@Override
	public void setDefault(Long id) {
		//方法一,将所有收货地址重置为非默认
		//然后将本次id对应地址设置为默认
		// UpdateWrapper<Address> wrapper = new UpdateWrapper<>();
		// wrapper.set("is_default",0)
		// 		.eq("user_id",UserHolder.getUser().getId());
		//
		// //参数说明:参数一:更新字段,参数二,更新条件
		// addressMapper.update(null,wrapper);
		// //该方法中的字段需要和数据库中的对应,lambda表达式中的方法不需要考虑此类问题,lambda表达式比较方便
		// wrapper.set("is_default",0)
		// 		.eq("user_id",UserHolder.getUser().getId())
		// 		.eq("id",id);
		// addressMapper.update(null,wrapper);
		
		//方式二
		Address address = new Address();
		address.setIsDefault(0);
		LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(Address::getUserId,UserHolder.getUser().getId());
		//该方法不会更新值为null的字段
		//如果将该方法的参数一添加为null,则需要使用.set方法进行指定添加字段
		addressMapper.update(address,wrapper);
		
		address.setIsDefault(1);
		address.setId(id);
		addressMapper.updateById(address);
	}
	
	@Override
	public void save(Address address) {
		address.setUserId(UserHolder.getUser().getId());
		//查询数据库,如果数据库中没有地址则直接设置为默认地址,如果有地址,则设置为非默认地址0
		LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(Address::getUserId,UserHolder.getUser().getId());
		List<Address> addressList = addressMapper.selectList(wrapper);
		
		if (addressList.size() < 1 || addressList == null) {
			//地址数量少于1,设置为默认地址
			address.setIsDefault(1);
		} else {
			//地址数量不少于1,设置为非默认地址
			address.setIsDefault(0);
		}
		addressMapper.insert(address);
	}
	
	@Override
	public List<Address> findList() {
		LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(Address::getUserId,UserHolder.getUser().getId());
		List<Address> addressList = addressMapper.selectList(wrapper);
		return addressList;
	}
}
